package com.bc.bcapi.service;

import com.alibaba.fastjson.JSONObject;
import com.bc.bcapi.dao.T_Gr_mytoken_detailMapper;
import com.bc.bcapi.dao.T_Gr_mytokenioMapper;
import com.bc.bcapi.dao.T_Gr_mytokenio_com_infoMapper;
import com.bc.bcmodel.domain.T_Gr_mytoken_detailModel;
import com.bc.bcmodel.domain.T_Gr_mytokenioModel;
import com.bc.bcmodel.domain.T_Gr_mytokenio_com_infoModel;
import com.bc.bcmodel.domain.T_Gr_mytokenio_linkModel;
import com.bc.commoncore.util.HttpUtil;
import com.bc.commoncore.util.JsonUtil;
import com.bc.commoncore.util.StringUtils;
import com.bc.commonentity.common.JsonResult;
import org.springframework.stereotype.Service;
import com.bc.bcmodel.resp.GenrationMyTokenTagsResp;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;

import com.bc.bcmodel.resp.GenrationMyTokenDetailResp;

/**
 * @Name ProjectGenerationMyTokenService
 * @Description TODO
 * @Author douhaichao
 * @Date 2018/11/28 9:49
 * @Version 1.0
 **/
@Service
public class ProjectGenerationMyTokenService {
    private String domain = "https://www.mytoken.io/";

    @Resource
    T_Gr_mytokenioMapper t_gr_mytokenioMapper;

    @Resource
    T_Gr_mytokenio_com_infoMapper t_gr_mytokenio_com_infoMapper;
    @Resource
    T_Gr_mytoken_detailMapper t_gr_mytoken_detailMapper;
    @Resource
    T_Gr_mytokenio_linkService t_gr_mytokenio_linkService;
    @Resource
    T_Gr_mytoken_detailService t_gr_mytoken_detailService;

    /**
     * 获取mytoken.io市值榜
     */
    public JsonResult generationMyTokenIO() {
        getDBData();
        return null;
    }

    private void SpinColumn() {
        List<T_Gr_mytokenio_linkModel> list = t_gr_mytokenio_linkService.getAllList(null);
        for (T_Gr_mytokenio_linkModel item :
                list) {
            String k = item.getK();
            String v = item.getV();
            Integer pid = item.getPid();
//            T_Gr_mytoken_link
            if (k.toLowerCase().equals("website")){

            }


        }

    }

    private void getDBData() {
        List<T_Gr_mytoken_detailModel> listdetail = t_gr_mytoken_detailService.getAllList(null);
        for (T_Gr_mytoken_detailModel item :
                listdetail) {
            String link = item.getLink();
            if (StringUtils.isEmpty(link)) {
                continue;
            }
            String[] arr = link.split("\\|");
            for (String item_arr :
                    arr) {
                if (StringUtils.isEmpty(item_arr)) continue;
                String[] linkData = item_arr.trim().split(" ");

                String k = linkData[0];
                String v = linkData[1];
                T_Gr_mytokenio_linkModel t_gr_mytokenio_linkModel = new T_Gr_mytokenio_linkModel();
                t_gr_mytokenio_linkModel.setPid(item.getPid());
                t_gr_mytokenio_linkModel.setK(v);
                t_gr_mytokenio_linkModel.setV(k);
                t_gr_mytokenio_linkService.insert(t_gr_mytokenio_linkModel);
            }
        }

//        List<T_Gr_mytokenioModel> list = t_gr_mytokenioMapper.getNotExistsData();
//        for (T_Gr_mytokenioModel item :
//                list) {
//            detail(item.getId());
//            System.out.print("完成:" + item.getId());
//        }
        System.out.print("完成所有了");
    }

    private void getListData() {
        String url = domain + "api/ticker/currencylist?page=1&size=1000&subject=market_cap&timestamp=1543369896750&code=e2ce011643d1428bd7a11aa27db6a9e8&platform=web_pc&v=1.0.0&language=zh_CN&legal_currency=CNY";
        list(url);
    }

    private void list(String url) {
        String listHtml = HttpUtil.sendGet(url, "");
        JSONObject json = JSONObject.parseObject(listHtml);
        String data = json.getString("data");
        JSONObject listStr = JSONObject.parseObject(data);
        String listData = listStr.getString("list");
        List<T_Gr_mytokenioModel> list =
                JsonUtil.str2list(listData, T_Gr_mytokenioModel.class);
        for (T_Gr_mytokenioModel item :
                list) {
            if (checkExistsById(item.getId())) {
                detail(item.getId());
                continue;
            }
            t_gr_mytokenioMapper.insert(item);
            List<T_Gr_mytokenio_com_infoModel> listhl = item.getCom_info();
            for (T_Gr_mytokenio_com_infoModel item_hl :
                    listhl) {
                item_hl.setPid(item.getId().intValue());
            }
            t_gr_mytokenio_com_infoMapper.insertBatch(listhl);

        }

    }

    private boolean checkExistsById(BigInteger id) {
        T_Gr_mytokenioModel t_gr_mytokenioModel = new T_Gr_mytokenioModel();
        t_gr_mytokenioModel.setId(id);
        int count = t_gr_mytokenioMapper.count(t_gr_mytokenioModel);
        return count > 0 ? true : false;
    }

    private void detail(BigInteger id) {
        String url = domain + "currency/" + id;
        String html = HttpUtil.sendGet(url, "");
        html = html.substring(html.indexOf("window.__NUXT__="));
        html = html.substring(0, html.indexOf("serverRendered"));
        String content = html.substring(html.indexOf("project_info"));
        content = content.substring(0, content.indexOf("kline_info"));
        content = content.replace("project_info\":", "");
        content = content.substring(0, content.lastIndexOf(",\""));
        JSONObject JSONtag = JSONObject.parseObject(content);
        String tag = JSONtag.getString("tags");
        List<GenrationMyTokenTagsResp> listGenrationMyTokenTagsResp = JsonUtil.str2list(tag, GenrationMyTokenTagsResp.class);
        String tag_name = "";
        for (GenrationMyTokenTagsResp item :
                listGenrationMyTokenTagsResp) {
            tag_name += item.getName() + ",";
        }
        JSONObject JSONContent = JSONObject.parseObject(content);
        String detailContent = JSONContent.getString("content");
//        detailContent = "\"content\":" + detailContent;
        detailContent = detailContent.replace("[{\"list\":[", "");
        detailContent = detailContent.substring(0, detailContent.lastIndexOf("],"));
//        JSONObject JSONDetailList = JSONObject.parseObject(detailContent);
//        String detailListStr = JSONDetailList.getString("list");
        detailContent = "[" + detailContent + "]";
        List<GenrationMyTokenDetailResp> listData = JsonUtil.str2list(
                detailContent, GenrationMyTokenDetailResp.class);

        T_Gr_mytoken_detailModel t_gr_mytoken_detailModel = new T_Gr_mytoken_detailModel();
        t_gr_mytoken_detailModel.setPid(id.intValue());
        t_gr_mytoken_detailModel.setTag(tag_name);
        for (GenrationMyTokenDetailResp item :
                listData) {
            String key = item.getKey();
            String contentData = item.getContent();
            if (key.equals("description")) {
                t_gr_mytoken_detailModel.setDescription(contentData);
            }
            if (key.equals("available_supply")) {
                t_gr_mytoken_detailModel.setAvailable_supply(contentData);
            }

            if (key.equals("total_supply")) {
                t_gr_mytoken_detailModel.setTotal_supply(contentData);
            }
            if (key.equals("available_supply_ratio")) {
                t_gr_mytoken_detailModel.setAvailable_supply_ratio(contentData);
            }
            if (key.equals("turnover_rate")) {
                t_gr_mytoken_detailModel.setTurnover_rate(contentData);
            }
            if (key.equals("max_supply")) {
                t_gr_mytoken_detailModel.setMax_supply(contentData);
            }
            if (key.equals("algorithm")) {
                t_gr_mytoken_detailModel.setAlgorithm(contentData);
            }
            if (key.equals("proof_type")) {
                t_gr_mytoken_detailModel.setProof_type(contentData);
            }
            if (key.equals("start_date")) {
                t_gr_mytoken_detailModel.setStart_date(contentData);
            }
            if (key.equals("online_exchange_number")) {
                t_gr_mytoken_detailModel.setOnline_exchange_number(contentData);
            }
            if (key.equals("wallet")) {
                t_gr_mytoken_detailModel.setWallet(contentData);
            }
            if (key.equals("link")) {
                contentData = contentData.replace("<a href=\"", "");
                contentData = contentData.replace("\">", " ");
                contentData = contentData.replace("</a>,", "|");
                t_gr_mytoken_detailModel.setLink(contentData);
            }

        }
        t_gr_mytoken_detailMapper.delete(id);
        t_gr_mytoken_detailMapper.insert(t_gr_mytoken_detailModel);
    }

}
